home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 February / CMCD0205.ISO / Software / Freeware / Programare / bluej / bluejsetup-203.exe / {app} / examples / shapes / Canvas.java < prev    next >
Text File  |  2004-12-19  |  7KB  |  222 lines

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.util.List;
  4. import java.util.*;
  5.  
  6. /**
  7.  * Canvas is a class to allow for simple graphical drawing on a canvas.
  8.  * This is a modification of the general purpose Canvas, specially made for
  9.  * the BlueJ "shapes" example. 
  10.  *
  11.  * @author: Bruce Quig
  12.  * @author: Michael Kolling (mik)
  13.  *
  14.  * @version: 1.6 (shapes)
  15.  */
  16. public class Canvas
  17. {
  18.     // Note: The implementation of this class (specifically the handling of
  19.     // shape identity and colors) is slightly more complex than necessary. This
  20.     // is done on purpose to keep the interface and instance fields of the
  21.     // shape objects in this project clean and simple for educational purposes.
  22.  
  23.     private static Canvas canvasSingleton;
  24.  
  25.     /**
  26.      * Factory method to get the canvas singleton object.
  27.      */
  28.     public static Canvas getCanvas()
  29.     {
  30.         if(canvasSingleton == null) {
  31.             canvasSingleton = new Canvas("BlueJ Shapes Demo", 300, 300, 
  32.                                          Color.white);
  33.         }
  34.         canvasSingleton.setVisible(true);
  35.         return canvasSingleton;
  36.     }
  37.  
  38.     //  ----- instance part -----
  39.  
  40.     private JFrame frame;
  41.     private CanvasPane canvas;
  42.     private Graphics2D graphic;
  43.     private Color backgroundColour;
  44.     private Image canvasImage;
  45.     private List objects;
  46.     private HashMap shapes;
  47.     
  48.     /**
  49.      * Create a Canvas.
  50.      * @param title  title to appear in Canvas Frame
  51.      * @param width  the desired width for the canvas
  52.      * @param height  the desired height for the canvas
  53.      * @param bgClour  the desired background colour of the canvas
  54.      */
  55.     private Canvas(String title, int width, int height, Color bgColour)
  56.     {
  57.         frame = new JFrame();
  58.         canvas = new CanvasPane();
  59.         frame.setContentPane(canvas);
  60.         frame.setTitle(title);
  61.         canvas.setPreferredSize(new Dimension(width, height));
  62.         backgroundColour = bgColour;
  63.         frame.pack();
  64.         objects = new ArrayList();
  65.         shapes = new HashMap();
  66.     }
  67.  
  68.     /**
  69.      * Set the canvas visibility and brings canvas to the front of screen
  70.      * when made visible. This method can also be used to bring an already
  71.      * visible canvas to the front of other windows.
  72.      * @param visible  boolean value representing the desired visibility of
  73.      * the canvas (true or false) 
  74.      */
  75.     public void setVisible(boolean visible)
  76.     {
  77.         if(graphic == null) {
  78.             // first time: instantiate the offscreen image and fill it with
  79.             // the background colour
  80.             Dimension size = canvas.getSize();
  81.             canvasImage = canvas.createImage(size.width, size.height);
  82.             graphic = (Graphics2D)canvasImage.getGraphics();
  83.             graphic.setColor(backgroundColour);
  84.             graphic.fillRect(0, 0, size.width, size.height);
  85.             graphic.setColor(Color.black);
  86.         }
  87.         frame.setVisible(visible);
  88.     }
  89.  
  90.     /**
  91.      * Draw a given shape onto the canvas.
  92.      * @param  referenceObject  an object to define identity for this shape
  93.      * @param  color            the color of the shape
  94.      * @param  shape            the shape object to be drawn on the canvas
  95.      */
  96.      // Note: this is a slightly backwards way of maintaining the shape
  97.      // objects. It is carefully designed to keep the visible shape interfaces
  98.      // in this project clean and simple for educational purposes.
  99.     public void draw(Object referenceObject, String color, Shape shape)
  100.     {
  101.         objects.remove(referenceObject);   // just in case it was already there
  102.         objects.add(referenceObject);      // add at the end
  103.         shapes.put(referenceObject, new ShapeDescription(shape, color));
  104.         redraw();
  105.     }
  106.  
  107.     /**
  108.      * Erase a given shape's from the screen.
  109.      * @param  referenceObject  the shape object to be erased 
  110.      */
  111.     public void erase(Object referenceObject)
  112.     {
  113.         objects.remove(referenceObject);   // just in case it was already there
  114.         shapes.remove(referenceObject);
  115.         redraw();
  116.     }
  117.  
  118.     /**
  119.      * Set the foreground colour of the Canvas.
  120.      * @param  newColour   the new colour for the foreground of the Canvas 
  121.      */
  122.     public void setForegroundColor(String colorString)
  123.     {
  124.         if(colorString.equals("red"))
  125.             graphic.setColor(Color.red);
  126.         else if(colorString.equals("black"))
  127.             graphic.setColor(Color.black);
  128.         else if(colorString.equals("blue"))
  129.             graphic.setColor(Color.blue);
  130.         else if(colorString.equals("yellow"))
  131.             graphic.setColor(Color.yellow);
  132.         else if(colorString.equals("green"))
  133.             graphic.setColor(Color.green);
  134.         else if(colorString.equals("magenta"))
  135.             graphic.setColor(Color.magenta);
  136.         else if(colorString.equals("white"))
  137.             graphic.setColor(Color.white);
  138.         else
  139.             graphic.setColor(Color.black);
  140.     }
  141.  
  142.     /**
  143.      * Wait for a specified number of milliseconds before finishing.
  144.      * This provides an easy way to specify a small delay which can be
  145.      * used when producing animations.
  146.      * @param  milliseconds  the number 
  147.      */
  148.     public void wait(int milliseconds)
  149.     {
  150.         try
  151.         {
  152.             Thread.sleep(milliseconds);
  153.         } 
  154.         catch (Exception e)
  155.         {
  156.             // ignoring exception at the moment
  157.         }
  158.     }
  159.  
  160.     /**
  161.      * Redraw ell shapes currently on the Canvas.
  162.      */
  163.     private void redraw()
  164.     {
  165.         erase();
  166.         for(Iterator i=objects.iterator(); i.hasNext(); ) {
  167.             ((ShapeDescription)shapes.get(i.next())).draw(graphic);
  168.         }
  169.         canvas.repaint();
  170.     }
  171.        
  172.     /**
  173.      * Erase the whole canvas. (Does not repaint.)
  174.      */
  175.     private void erase()
  176.     {
  177.         Color original = graphic.getColor();
  178.         graphic.setColor(backgroundColour);
  179.         Dimension size = canvas.getSize();
  180.         graphic.fill(new Rectangle(0, 0, size.width, size.height));
  181.         graphic.setColor(original);
  182.     }
  183.  
  184.  
  185.     /************************************************************************
  186.      * Inner class CanvasPane - the actual canvas component contained in the
  187.      * Canvas frame. This is essentially a JPanel with added capability to
  188.      * refresh the image drawn on it.
  189.      */
  190.     private class CanvasPane extends JPanel
  191.     {
  192.         public void paint(Graphics g)
  193.         {
  194.             g.drawImage(canvasImage, 0, 0, null);
  195.         }
  196.     }
  197.     
  198.     /************************************************************************
  199.      * Inner class CanvasPane - the actual canvas component contained in the
  200.      * Canvas frame. This is essentially a JPanel with added capability to
  201.      * refresh the image drawn on it.
  202.      */
  203.     private class ShapeDescription
  204.     {
  205.         private Shape shape;
  206.         private String colorString;
  207.  
  208.         public ShapeDescription(Shape shape, String color)
  209.         {
  210.             this.shape = shape;
  211.             colorString = color;
  212.         }
  213.  
  214.         public void draw(Graphics2D graphic)
  215.         {
  216.             setForegroundColor(colorString);
  217.             graphic.fill(shape);
  218.         }
  219.     }
  220.  
  221. }
  222.